package com.qfedu.edu.messagecenterserver.mqtt.observer.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.qfedu.edu.manager.RedisManager;
import com.qfedu.edu.messagecenterserver.mapper.ExceptionBusinessMapper;
import com.qfedu.edu.mqtt.data.MqttMessage;
import com.qfedu.edu.messagecenterserver.mqtt.observer.base.Observer;
import com.qfedu.edu.messagecenterserver.mqtt.observer.base.SubjectHardware;
import com.qfedu.edu.pojo.ExceptionBusiness;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author xiaobobo
 * @title: SubjectImpl
 * @projectName cd-fy-2401-third-project-parent
 * @description: 这个是咋们的这个观察者的调用者
 * @date 2024/10/9  17:37
 */
@Component
@Slf4j
public class SubjectImplHardware implements SubjectHardware {

    @Autowired
    private List<Observer> observers;

    @Autowired
    private RedisManager redisManager;

    @Autowired
    private ExceptionBusinessMapper exceptionBusinessMapper;

    @Override
    public void notifyHandler(MqttMessage mqttMessage) {
        try {
            for (int i = 0; i < observers.size(); i++) {
                Observer observer = observers.get(i);
                observer.notifyHandler(mqttMessage);
            }
            try {
                recordStatus(mqttMessage,1);
            }catch (Exception err){
                //记录操作到日志
                log.error(JSON.toJSONString(mqttMessage)+"消息的状态是:1记录到Redis失败....");
            }
        } catch (Exception err) {
            try {
                recordStatus(mqttMessage,1);
            }catch (Exception err1){
                //记录操作到日志
                log.error(JSON.toJSONString(mqttMessage)+"消息的状态是:2记录到Redis失败....");
            }
        }
        log.info("本轮数据的处理完成....");
    }

    /**
     * 记录状态的方法
     * @param mqttMessage
     * @param status
     */
    private void recordStatus(MqttMessage mqttMessage,Integer status) {
       /* //如果执行到了这里 那么说明消费失败...
        Integer mqttId = mqttMessage.getMessageHeader().getMqttId();
        //然后存储到数据库   String queue:msg:status:mqttId   状态  1、成功   2、失败
        //构建key
        String key = RedisConstance.getQueueInfoConsumerStatusPrefix() + mqttId;
        redisManager.addObject(key, status, 30, TimeUnit.DAYS);*/
        //说明业务执行成功....
        //删除数据库中的记录数据
        Integer mqttId = mqttMessage.getMessageHeader().getMqttId();
        if(status==1){
            //通过消息的id去删除数据
            UpdateWrapper<ExceptionBusiness> exceptionBusinessUpdateWrapper = new UpdateWrapper<>();
            exceptionBusinessUpdateWrapper.eq("mqtt_id",mqttId);
            //删除数据
            exceptionBusinessMapper.delete(exceptionBusinessUpdateWrapper);
        }else{
            //修改这个数据为失败
            //先查询
            //通过消息的id去删除数据
            QueryWrapper<ExceptionBusiness> exceptionBusinessUpdateWrapper = new QueryWrapper<>();
            exceptionBusinessUpdateWrapper.eq("mqtt_id",mqttId);
            ExceptionBusiness exceptionBusiness = exceptionBusinessMapper.selectOne(exceptionBusinessUpdateWrapper);
            exceptionBusiness.setStatus(2);
            exceptionBusinessMapper.updateById(exceptionBusiness);
        }
    }
}
